<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class JSONDataRepresentation {

    private $mJSONRep = array();
    private $mIsConfigShowed = true;
    private $mIsErrorShowed = false;
    private $mConfig = array();
    private $mData = array();
    private $mError = array();
    private $mRowPointer;

    public function __construct($pParams = null) {
        
    }

    public function CreateNewData() {
        $tFungsi = new Fungsi();

        $tFungsi->CalibrateArray($this->mData);
        $tIndex = $tFungsi->GetArrayIndexOf($this->mData);

        //CHECK: JIKA nilai pointer belum memiliki nilai
        if (!empty($this->mRowPointer)) {
            //Karena nilai pointer sudah ada, maka buat yang baru
            $tIndex++;
        }

        $this->mRowPointer = $tIndex;
    }

    public function InsertConfig($tParamater, $tValue) {
        if (isset($this->mConfig[$tParamater]) && is_array($tValue)) {
            foreach ($tValue as $tKey => $tVal) {
                $this->mConfig[$tParamater][$tKey] = $tVal;
            }
        }
        else
            $this->mConfig[$tParamater] = $tValue;
    }

    public function InsertData($tKey, $tValue) {
        $this->mData[$this->mRowPointer][$tKey] = $tValue;
    }

    public function PrintJSONDataRepresentation() {
        $tOutput = array();
        if ($this->mIsConfigShowed == true) {
            $this->mJSONRep['Config'] = $this->mConfig;
            $tOutput[0] = $this->mJSONRep['Config'];
            print_r(json_encode($tOutput));
        }
        if ($this->mIsErrorShowed == true) {
            $this->mJSONRep['Error'] = $this->mError;
            $tOutput[0] = $this->mJSONRep['Error'];
            print_r(json_encode($tOutput));
        }

        $this->mJSONRep['Data'] = $this->mData;
        
        $tRow=0;
        foreach ($this->mData as $tData) {
            foreach ($tData as $tFieldName => $tValue) {
                $tResult[$tRow][$tFieldName] = $tValue;
            }
            $tRow++;
        }
        print_r(json_encode($tResult));
    }

    //MUTUTATOR
    public function SetConfigObjectShowed($pBool) {
        $this->mIsConfigShowed = $pBool;
    }

    public function SetErrorObjectShowed($pBool) {
        $this->mIsErrorShowed = $pBool;
    }

    public function SetErrorTitle($pTitle) {
        $this->mIsErrorShowed = true;
        $this->mError['ErrorTitle'] = $pTitle;
    }

    public function SetErrorMessage($pMessage) {
        $this->mIsErrorShowed = true;
        $this->mError['ErrorMessage'] = $pMessage;
    }

    public function SetErrorSection($pSection) {
        $this->mIsErrorShowed = true;
        $this->mError['ErrorSection'] = $pSection;
    }

    public function SetErrorCode($pCode) {
        $this->mIsErrorShowed = true;
        $this->mError['ErrorCode'] = $pCode;
    }

}
